<?php


namespace app\common\library;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

use common;
use hiredis;

class UserToken
{

    static function expire_time($user_type)
    {
        if ($user_type == OPER_TYPE_CS || $user_type == OPER_TYPE_CC) {
            return 3600 * 3;
        } else {
            return 3600 * 24 * 30;
        }
    }

    function mgr_reg_token($id, $token)
    {
        $key = 'TOKENS|' . $id;
        hiredis::db()->hSet($key, $token, date('Y-m-d H:i:s'));
    }

    function mgr_delete_token($id, $token)
    {
        $key = 'TOKENS|' . $id;
        hiredis::db()->hDel($key, $token);
    }

    function mgr_delete_all_token($id)
    {
        $key = 'TOKENS|' . $id;
        $tokens = hiredis::db()->hKeys($key);
        foreach ($tokens as $token) {
            $k = "TOKEN|" . $token;
            hiredis::db()->del($k);
        }
        hiredis::db()->del($key);
    }

    function update($token, $data)
    {
        $key = "TOKEN|" . $token;
        hiredis::db()->hMset($key, $data);
    }

    function create_user_token($userinfo)
    {
        $paramlist = [
            'id' => 'string',
            'name' => 'string',
            'phone' => 'string',
            'channel' => 'string',
        ];

        $info = common::type_conver($userinfo, $paramlist);
        $info['user_type'] = OPER_TYPE_USER;
        $info['create_time'] = date('Y-m-d H:i:s');
        //$info['channel'] = 'wxmp';
        $token = common::sn();

        $key = "TOKEN|" . $token;
        hiredis::db()->hMset($key, $info);
        hiredis::db()->expire($key, self::expire_time(OPER_TYPE_USER));
        return $token;
    }

    function create_cc_token($userinfo, $token = null)
    {
        $paramlist = [
            'id' => 'string',
            'name' => 'string',
            'phone' => 'string',
            'cc_role_id' => 'string',
            'city_id' => 'string',
            'acl' => 'string',
            'channel' => 'string',
        ];

        $info = common::type_conver($userinfo, $paramlist);
        $info['user_type'] = OPER_TYPE_CC;
        $info['channel'] = 'CC';
        $info['role_id'] = $userinfo['cc_role_id'];
        $info['create_time'] = date('Y-m-d H:i:s');
        if (empty($token)) {
            $token = common::sn();
        }

        $key = "TOKEN|" . $token;
        hiredis::db()->hMset($key, $info);
        hiredis::db()->expire($key, self::expire_time(OPER_TYPE_CC));

        $this->mgr_reg_token($userinfo['id'], $token);
        return $token;
    }

    function create_cs_token($userinfo, $token = null)
    {
        $paramlist = [
            'id' => 'string',
            'name' => 'string',
            'phone' => 'string',
            'cc_role_id' => 'string',
            //  'city_id' => 'string',
            'acl' => 'string',
            'channel' => 'string',
        ];

        $info = common::type_conver($userinfo, $paramlist);
        $info['user_type'] = OPER_TYPE_CS;
        $info['role_id'] = $userinfo['cs_role_id'];
        $info['channel'] = 'CS';
        $info['create_time'] = date('Y-m-d H:i:s');

        if (empty($token)) {

        }
        $token = common::sn();

        $key = "TOKEN|" . $token;
        hiredis::db()->hMset($key, $info);
        hiredis::db()->expire($key, self::expire_time(OPER_TYPE_CS));

        $this->mgr_reg_token($userinfo['id'], $token);
        return $token;
    }

    function create_ops_token($userinfo, $token = null)
    {
        $paramlist = [
            'id' => 'string',
            'name' => 'string',
            'phone' => 'string',
            'ops_role_id' => 'string',
            'city_id' => 'string',
            'acl' => 'string',
            'channel' => 'string',
        ];

        $info = common::type_conver($userinfo, $paramlist);
        $info['user_type'] = OPER_TYPE_OPS;
        $info['role_id'] = $userinfo['ops_role_id'];
        $info['channel'] = 'OPS';
        if (empty($token)) {
            $token = common::sn();
        }
        $info['create_time'] = date('Y-m-d H:i:s');

        $key = "TOKEN|" . $token;
        hiredis::db()->hMset($key, $info);
        hiredis::db()->expire($key, self::expire_time(OPER_TYPE_OPS));

        $this->mgr_reg_token($userinfo['id'], $token);
        return $token;
    }

    function query_info($token)
    {
        $key = "TOKEN|" . $token;
        $kvs = hiredis::db()->hGetAll($key);
        if (empty($kvs))
            return null;

        $paramlist = null;
        if (intval($kvs['user_type']) == OPER_TYPE_USER) {
            $paramlist = [
                'id' => 'string',
                'name' => 'string',
                'phone' => 'string',
                'user_type' => 'int',
                'channel' => 'string',
            ];
        } else if (intval($kvs['user_type']) == OPER_TYPE_CC) {
            $paramlist = [
                'id' => 'string',
                'name' => 'string',
                'phone' => 'string',
                'cc_role_id' => 'string',
                'role_id' => 'string',
                'city_id' => 'string',
                'acl' => 'array',
                'channel' => 'string',
                'user_type' => 'int',
            ];
        } else if (intval($kvs['user_type']) == OPER_TYPE_CS) {
            $paramlist = [
                'id' => 'string',
                'name' => 'string',
                'phone' => 'string',
                'cs_role_id' => 'string',
                'role_id' => 'string',
                // 'city_id' => 'string',
                'acl' => 'array',
                'channel' => 'string',
                'user_type' => 'int',
            ];
        } else if (intval($kvs['user_type']) == OPER_TYPE_OPS) {
            $paramlist = [
                'id' => 'string',
                'name' => 'string',
                'phone' => 'string',
                'ops_role_id' => 'string',
                'role_id' => 'string',
                'city_id' => 'string',
                'acl' => 'array',
                'channel' => 'string',
                'user_type' => 'int',
            ];
        } else {
            return [];
        }

        $r = rand(0, 20);
        if ($r == 0) {
            hiredis::db()->expire($key, self::expire_time($kvs['user_type']));
        }
        return common::type_conver($kvs, $paramlist);
    }

    function delete($id, $token)
    {
        $key = "TOKEN|" . $token;
        hiredis::db()->delete($key);
        $this->mgr_delete_token($id, $token);
    }

}